define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {

            };
        },
        getAreaDefault:function () {
            var self=this;
            servers.targets.area({type:1},function(res){
                var list=[];
                if(res.success){
                    list=$.map(res.data.area,function(info){
                        return {id:info.id, text:info.name, originalName:info.name, parent:info.parentId.toString()==="0"?"#":info.parentId.toString()}
                    });
                }
                res.data=list;
                self.set({dataAreaDefault:res});
                self.trigger('bind:AreaDefault');
            });
        },
        getAreaGroup:function () {
            var self=this;
            servers.targets.area({type:2},function(res){
                var totalList=[];
                if(res.success){
                    var arr=[{key:"core",name:"核心城市"},{key:"important",name:"重点城市"},{key:"other",name:"其他城市"}];
                    $.map(arr,function (topInfo) {
                        var jsonAll={};
                        $.each(res.data[topInfo.key],function(i,info){
                            var id=info.id.toString();
                            var parentId=info.parentId.toString();
                            parentId=parentId==="0"?"#":parentId;
                            jsonAll[id]={id:id,text:info.name,originalName:info.name,parent:parentId};
                        });
                        var parent=[];
                        var fakeParent=[];
                        var children=[];
                        $.each(jsonAll,function(k,info){
                            if(info.parent==="#"){
                                parent.push(info);
                            }else{
                                if(jsonAll.hasOwnProperty(info.parent)){
                                    children.push(info);
                                }else{
                                    fakeParent.push(info);
                                }
                            }
                        });
                        var topId=topInfo.key+"_0000";
                        totalList.push({id:topId,text:topInfo.name,originalName:topInfo.name,parent:"#"});
                        var topLst=[];
                        if(parent.length>0){
                            $.each(parent,function(k,info){info.parent=topId});
                            topLst=topLst.concat(parent);
                        }
                        if(fakeParent.length>0){
                            $.each(fakeParent,function(k,info){info.parent=topId});
                            topLst=topLst.concat(fakeParent);
                        }
                        if(children.length>0){
                            topLst=topLst.concat(children);
                        }
                        totalList=totalList.concat(topLst);
                    });
                }
                res.data=totalList;
                self.set({dataAreaGroup:res});
                self.trigger('bind:AreaGroup');
            });
        },
        getCrowd:function(){
            var self=this;
            servers.targets.crowd(function(res){
                var education=[],marriage=[],gender=[];
                if(res.success){
                    if(res.data.education){education=$.ExtCommon.convertListByPara(res.data.education)}
                    if(res.data.marriage){marriage=$.ExtCommon.convertListByPara(res.data.marriage)}
                    if(res.data.gender){gender=$.ExtCommon.convertListByPara(res.data.gender)}
                }
                self.set({dataGender:servers.getSendSuccess(gender)});
                self.trigger('bind:Gender');

                self.set({dataEducation:servers.getSendSuccess(education)});
                self.trigger('bind:Education');

                self.set({dataMarriage:servers.getSendSuccess(marriage)});
                self.trigger('bind:Marriage');
            });
        },
        getDevice:function(){
            var self=this;
            servers.targets.device(function(res){
                var telCom=[],os=[],connection=[];
                if(res.success){
                    if(res.data["networkOperator"]){telCom= $.ExtCommon.convertListByPara(res.data["networkOperator"])}
                    if(res.data.os){os=$.ExtCommon.convertListByPara(res.data.os)}
                    if(res.data["networkType"]){connection=$.ExtCommon.convertListByPara(res.data["networkType"])}
                }
                self.set({dataTelCom:servers.getSendSuccess(telCom)});
                self.trigger('bind:TelCom');

                self.set({dataOs:servers.getSendSuccess(os)});
                self.trigger('bind:Os');

                self.set({dataConnection:servers.getSendSuccess(connection)});
                self.trigger('bind:Connection');
            });
        },
        getInterest:function () {
            var self=this;
            servers.targets.interest(function(res){
                var list=[];
                if(res.success){
                    list=$.map(res.data,function(info){
                        return {id:info.id, text:info.name, originalName:info.name, parent:info.parentId===0?"#":info.parentId};
                    });
                }
                res.data=list;
                self.set({dataInterest:res});
                self.trigger('bind:Interest');
            });
        },
        getAppAction:function () {
            var self=this;
            servers.targets.appAction(function(res){
                var appAction=[],userAction=[];
                if(res.success){
                    if(res.data["appCategroys"]){
                        appAction=$.map(res.data["appCategroys"],function(info){
                            return {id:info.id, text:info.name, originalName:info.name, parent:info.parentId==="0"?"#":info.parentId};
                        });
                    }
                    if(res.data["appActIds"]){userAction=$.ExtCommon.convertListByPara(res.data["appActIds"])}

                }
                self.set({dataAppAction:servers.getSendSuccess(appAction)});
                self.trigger('bind:AppAction');

                self.set({dataUserAction:servers.getSendSuccess(userAction)});
                self.trigger('bind:UserAction');
            });
        }
    });
});